package com.lecture.java8.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class InheritableThreadLocalExample {
    private static final InheritableThreadLocal<String> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        // 在主线程中设置InheritableThreadLocal的值
        threadLocal.set("Main thread value");
        System.out.println("Main thread value: " + threadLocal.get());

        // 提交任务给线程池执行
        executorService.submit(() -> {
            // 子线程继承父线程的InheritableThreadLocal值
            System.out.println("Child thread value: " + threadLocal.get());
        });



        executorService.shutdown();
    }
}
